﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Command.building
{
    partial class SangoBuildingHandler
    {
        public building_queuecooldown building_queuecooldown()
        {
            string userCode = SangoManager.Authentication();

            building_queuecooldown response = ReflectionManager.CreatePojo<building_queuecooldown>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Queue usrQueue = UsrQueueDao.tryGetRunningQueue(profile.Usercode, CtiQueueTypecodeCollection.BUILDING_UPGRADE);

            if (usrQueue == null)
            {
                response.Message = "无冷却队列";
                return response;
            }

            Entities.Sys_Queue sysQueue = SysQueueHelper.searchByCode(usrQueue.Queueserialcode);

            int goldprice = QueueFormular.getQueueCooldownGoldPrice(usrQueue, sysQueue);

            if (goldprice <= 0)
            {
                response.Message = "无须冷却队列";
                return response;
            }

            if (!UsrProfileDao.tryDecreaseGoldValue(profile, goldprice))
            {
                response.Message = "金币不足";
                return response;
            }

            UsrQueueDao.UsrQueueCooldown(usrQueue);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                command.Update(usrQueue);

                command.Update(profile);

                command.Commit();
            });


            response.queue = UsrQueueFacade.ToQueue(usrQueue);
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.Succeed = true;
            return response;
        }


    }
    public interface building_queuecooldown : ISangoResponse
    {
        player_queue queue { get;set;}
        player_profile profile { get;set;}

    }
}
